package com.zhiyou100.controller;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import com.zhiyou100.entity.ResponseEntity;
import com.zhiyou100.entity.UserAccount;
import com.zhiyou100.entity.UserInfo;
import com.zhiyou100.service.inter.UserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户模块-账户
 * RestController = ResponseBody + Controller
 * @author yang
 */
@RestController
public class UserAccountController {
    @Autowired
    private UserAccountService service;

    @PutMapping("/user/account/add.action")
    public ResponseEntity<Object> addAddress(@RequestBody UserAccount account, HttpServletRequest request) {
        if (ObjectUtil.isNull(account)) {
            // 为 null
            return new ResponseEntity<>(500, "account 为空", null);
        }
        // 从域中获取
        final UserInfo userInfo = (UserInfo) request.getSession().getAttribute("userInfo");
        // 设置 id
        account.setAccountUserId(userInfo.getUserId());
        // 插入
        service.insert(account);
        // 响应
        return new ResponseEntity<>(200, "添加成功", account);
    }

    @PostMapping("/user/account/update.action")
    public ResponseEntity<Object> updateAddress(@RequestBody UserAccount account, HttpServletRequest request) {
        if (ObjectUtil.isNull(account)) {
            // 为 null
            return new ResponseEntity<>(500, "account 为空", null);
        }
        // 从域中获取
        final UserInfo userInfo = (UserInfo) request.getSession().getAttribute("userInfo");
        // 设置 id
        account.setAccountUserId(userInfo.getUserId());
        // 插入
        service.updateById(account);
        // 响应
        return new ResponseEntity<>(200, "修改成功", account);
    }

    @PostMapping("/user/account/delete.action")
    public ResponseEntity<Object> deleteAddress(@RequestParam("accountId") Integer id) {
        if (ObjectUtil.isNull(id)) {
            // 为 null
            return new ResponseEntity<>(500, "id 为空", null);
        }
        final UserAccount account = service.selectById(id);
        if (ObjectUtil.isNull(account)) {
            // 没有 该账户
            return new ResponseEntity<>(500, "没有该账户", null);
        }
        service.deleteById(id);
        // 响应
        return new ResponseEntity<>(200, "删除成功", id);
    }


    @GetMapping("/user/account/select/list/{id}.action")
    public ResponseEntity<Object> deleteAddress(@PathVariable String id) {
        if (ObjectUtil.isNull(id)) {
            // 为 null
            return new ResponseEntity<>(500, "id 为空", null);
        }
        final List<UserAccount> userAccounts = service.selectByMap(Dict.create()
                .set("account_user_id", id));
        if (ObjectUtil.isNull(userAccounts)) {
            // 该 用户 ID 没有 账户
            return new ResponseEntity<>(500, "此用户没有 账户", id);
        }
        // 响应
        return new ResponseEntity<>(200, "此用户的所有账户", userAccounts);
    }
}
